<template>
  <div class="table">
    <el-alert
        :title="'共  '+tableData.counts+'  条记录'"
        type="info"
        show-icon
        :closable='false'>
      </el-alert>
      <el-table
      :data="tableData.items"
      style="width: 100%">
      <el-table-column
      align="center"
        label="序号"
        width="100">
      <template slot-scope="scope">
        <span>{{scope.row.id}}</span>
      </template>
      </el-table-column>
      <el-table-column
      align="center"
        prop="number"
        label="企业编号"
        >
      </el-table-column>
      <el-table-column
      align="center"
        width="120"
        prop="shortName"
        label="企业简称">
      </el-table-column>
      <el-table-column
      align="center"
        width="120"
        prop="tags"
        label="标签">
      </el-table-column>
      <el-table-column
      align="center"
       width="120"
        prop="creatorID"
        label="创建者">
      </el-table-column>
      <el-table-column
      align="center"
        prop="addDate"
        label="创建日期">
        <template v-slot="{row}">
          {{row.addDate|parseTimeByString}}
        </template>
      </el-table-column>
      <el-table-column
      align="center"
        prop="remarks"
        label="备注">
      </el-table-column>
      <el-table-column
      align="center"
        width="120"
        prop="state"
        label="状态">
        <template v-slot="{row}">
          {{row.state===1?'启用':'禁用'}}
        </template>
      </el-table-column>
      <el-table-column
      align="center"
        prop="address"
        label="操作">
        <template  v-slot="{row}">
        <el-button type="primary" plain circle icon="el-icon-edit" @click="handleUpdate(row)" :disabled="row.state===0"></el-button>
        <el-tooltip content="启用" placement="top" v-if="row.state===0">
          <el-button type="success" circle plain icon="el-icon-check" @click="handleStatus(row)"></el-button>
        </el-tooltip>
         <el-tooltip content="禁用" placement="top" v-else>
          <el-button type="warning" circle  plain icon="el-icon-close"  @click="handleStatus(row)"></el-button>
        </el-tooltip>
        <el-button type="danger" plain circle icon="el-icon-delete" @click="handleDelete(row.id)" :disabled="row.state===0"></el-button>
      </template>
      </el-table-column>
    </el-table>
     <div class="pagination">
       <el-pagination
     background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="Number(formDate.Pagesize)"
      layout="prev, pager, next,sizes,jumper"
      :total="tableData.counts">
    </el-pagination>
     </div>
  </div>
</template>

<script>
import { status } from '@/api/hmmm/constants.js'
import { disabled, remove } from '@/api/hmmm/companys.js'

export default {
  name: 'tables',
  props: {
    tableData: {
      type: [Array, Object],
      required: true
    },
    CurrentChange: {
      type: Object,
      required: true
    },
    formDate: {
      type: [Array, Object],
      required: true
    }
  },
  data () {
    return {
      currentPage: undefined

    }
  },
  created () {

  },
  computed: {
    status () {
      return status
    }
  },
  methods: {
    // 删除
    async handleDelete (id) {
      this.$confirm('此操作将永久删除该用户，是否继续', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await remove(id)
          this.$message.success('已成功删除')
          this.$emit('onState', this.tableData)
        } catch (error) {
          this.$message.error('操作失败')
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
        this.$emit('onState', this.tableData)
      })
    },
    handleSizeChange (val) {
      this.$emit('handleSChange', val)
    },
    handleCurrentChange (val) {
      this.$emit('handleCChange', val)
    },
    // 编辑
    handleUpdate (row) {
      this.$emit('companysUpdate', row)
    },

    // 状态
    async handleStatus (row) {
      if (row.state === 0) {
        this.tit = '启用'
      } else {
        this.tit = '禁用'
      }
      // const data = {
      //   id: row.id,
      //   state: row.state
      // }

      await this.$confirm('已成功' + this.tit + ', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          if (row.state === 0) {
            row.state = 1
            this.tit = '启用'
          } else {
            row.state = 0
            this.tit = '禁用'
          }
          const data = {
            id: row.id,
            state: row.state
          }
          await disabled(data)

          this.$message.success('已成功' + this.tit)
          this.$emit('onState', this.tableData)
        } catch (error) {
          this.$message.error('操作失败')
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
        this.$emit('onState', this.tableData)
      })
    }
  }
}
</script>

<style scoped lang="scss">
.el-table{
  font-size: 16px;}
.pagination{
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
.table{
  margin-top: 40px;
}
::v-deep .alert {
  font-size: 16px;
  background-color: #fff !important;
.el-alert__icon{
  font-size: 24px !important;
  margin-right: 10px;
}
.el-alert__title{
  font-size: 18px;
  color: black;
}
}

</style>
